package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 按钮标签
 * @author zhanglong
 *
 */
public class ButtonTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	private String id;
	private String name;
	private String value;
	private String onclick;
	private String icon;
	private String plain;
	private String color="";
	@Override
	public int doStartTag() throws JspException {
		try{
			JspWriter out = pageContext.getOut();
			StringBuffer sBF = new StringBuffer();
			sBF.setLength(0);
			sBF.append(" <a class=\"easyui-linkbutton ");
			if(!(this.getParent().getParent() instanceof DataGridTag && (this.color==null||"".equals(this.color)))){
				this.color="info";
			}
			if("info".equals(color)){
				sBF.append("button-info");
			}else if("primary".equals(color)){
				sBF.append("button-default");
			}else if("success".equals(color)){
				sBF.append("button-success");
			}else if("warning".equals(color)){
				sBF.append("button-warning");
			}else if("danger".equals(color)){
				sBF.append("button-danger");
			}else if("inverse".equals(color)){
				sBF.append("button-inverse");
			}else{
				sBF.append(color);
			}
			sBF.append("");
			sBF.append("\"");
			if(!(id==null||"".equals(id))){
				sBF.append(" id = \"" +id + "\" ");
			}
			if(!(name==null||"".equals(name))){
				sBF.append(" name = \"" +name + "\" ");
			}
		
			if(!(onclick==null||"".equals(onclick))){
				sBF.append(" onclick=\"" + onclick +"\" ");
			}
			if(!(icon==null||"".equals(icon))){
				sBF.append(" iconCls =\""+icon+"\" ");
			}
			if(this.getParent() instanceof DataGridToolBarTag){
				this.plain = "true";
			}
			if(!(plain==null||"".equals(plain))&&"true".endsWith(plain)){
				sBF.append(" plain =\""+plain+"\" ");
			}
			sBF.append(">");
			if(!(value==null)||"".equals(value)){
				sBF.append(value);//按钮上的文本
			}
			sBF.append("</a>");
			//如果在formtable里面就加上表格标签
			
			if(this.getParent() instanceof DataGridToolBarTag){
				sBF.append("|");//datagrid toolbar上的按钮之间加上 |
			}
			if(this.getParent() instanceof FormTableTag){
				out.print("<td style=\"text-align:right\">"+sBF.toString()+"</td>" );
			}else{
				out.print(sBF.toString());
			}
		}catch(IOException e){
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	
	//get and set 方法
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getPlain() {
		return plain;
	}

	public void setPlain(String plain) {
		this.plain = plain;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
	
	
}
